<?php

namespace App\Http\Controllers\Cooperate;

use App\Helpers\Tools;
use App\Http\Controllers\ApiController;
use App\Models\User;
use Illuminate\Support\Facades\Redis;

class UserController extends ApiController
{
    public function appendInfo($id)
    {
        $data = request()->all(['sex', 'age', 'occupation', 'hobby', 'is_firend', 'firend_number', 'firend_sex', 'goal']);

        $user = array_merge($data, unserialize(Redis::get('user:' . $id)));

        Redis::lpush('user:append:list', serialize($data));

        if ($user) {
            // 存入redis
            Redis::set('user:' . $user['id'], serialize($user));

            if ($data['is_firend'] == 1) {
                # 如果有搭档
                // 1 存进redis
                // 2 提示搭档是否同意
                // 3 判断搭档编号是否存在，存进redis，如果不存在，则提示
                $numberId = Redis::get('number:id');
                if ($numberId >= $data['firend_number']) {
                    // 判断搭档是否已有队友
                    $troopsNumber = Redis::get("user:troops:number:" . $data['firend_number']);
                    if ($troopsNumber) {
                        return Tools::error('已经有队友');
                    }
                    // 添加进有序集
                    Redis::zadd('firends', $data['firend_number'], $user['number']);
                } else {
                    return Tools::error('该编号用户不存在，请重新填写');
                }
            } else {
                # 如果没搭档
                // 添加进匹配集
                Redis::zadd('match', $data['firend_sex'], $user['number']);
            }

            return Tools::success('匹配提交进后台成功');
        } else {
            return Tools::error('匹配提交进后台失败');
        }
    }

    public function info($id)
    {
        $userInfo = unserialize(Redis::get("user:$id"));

        return Tools::setData($userInfo);
    }
}